﻿#include "AppMainWindow.h"
#include "ui_AppMainWindow.h"
//插件相关
#include "DAWorkFlowPluginManager.h"
#include "DAPluginManager.h"
#include "DAAbstractPlugin.h"
#include "DAAbstractNodePlugin.h"
//界面相关
#include "DAAppCore.h"
#include "DAAppUI.h"
#include "DAAppDockingArea.h"
#include "DAAppRibbonArea.h"
//对话框
#include "DAPluginManagerDialog.h"
//节点相关
#include "DANodeMetaData.h"
#include "DAAbstractNodeFactory.h"
#include "DAAbstractNodeWidget.h"

//
#include "DAWorkFlowNodeListWidget.h"
#include "DAWorkFlowOperateWidget.h"

//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// AppMainWindow
//===================================================
AppMainWindow::AppMainWindow(QWidget* parent) : SARibbonMainWindow(parent)
//, ui(new Ui::AppMainWindow)
{
    // ui->setupUi(this);
    //建立ribbonArea，此函数的构造函数会生成界面
    DAAppCore& core = DAAppCore::getInstance();
    core.createUi(this);
    m_ui       = qobject_cast< DAAppUI* >(core.getUiInterface());
    m_dockArea = qobject_cast< DAAppDockingArea* >(m_ui->dockingArea());
    //首次调用此函数会加载插件，可放置在main函数中调用
    init();
    m_dockArea->hideDockWidget(m_dockArea->getWorkflowNodeListWidget());
    retranslateUi();
    showMaximized();
}

AppMainWindow::~AppMainWindow()
{
    //    delete ui;
}

void AppMainWindow::retranslateUi()
{
    m_ui->retranslateUi();
}

void AppMainWindow::changeEvent(QEvent* e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        retranslateUi();
        break;

    default:
        break;
    }
}

void AppMainWindow::setupNodeListWidget()
{
    DAWorkFlowPluginManager& plugin          = DAWorkFlowPluginManager::instance();
    QList< DAAbstractNodeFactory* > factorys = plugin.getNodeFactorys();
}

void AppMainWindow::init()
{
    DAWorkFlowPluginManager::instance();
    m_workflow = new DADataWorkFlow(this);
    initUI();
    initConnect();
}

void AppMainWindow::initUI()
{
    //设置dock

    DAWorkFlowPluginManager& pluginmgr       = DAWorkFlowPluginManager::instance();
    QList< DAAbstractNodeFactory* > factorys = pluginmgr.getNodeFactorys();
    //提取所有的元数据
    QList< DANodeMetaData > nodeMetaDatas;

    for (DAAbstractNodeFactory* factory : qAsConst(factorys)) {
        nodeMetaDatas += factory->getNodesMetaData();
        //注册工厂
        m_workflow->registFactory(factory);
        qDebug() << "registFactory:" << factory->getPrototypes();
    }
    //对所有元数据按group进行筛分
    QMap< QString, QList< DANodeMetaData > > groupedNodeMetaDatas;

    for (const DANodeMetaData& md : qAsConst(nodeMetaDatas)) {
        groupedNodeMetaDatas[ md.getGroup() ].append(md);
    }
    //把数据写入toolbox
    m_dockArea->getWorkflowNodeListWidget()->addItems(groupedNodeMetaDatas);
    m_dockArea->getWorkFlowOperateWidget()->setNodeFactory(m_workflow);
}

void AppMainWindow::initConnect()
{
}
